package com.zby.ss.core.util;

import com.zby.ss.util.ArrayUtil;
import com.zby.ss.util.CollectionUtil;
import com.zby.ss.util.StringHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.security.GeneralSecurityException;
import java.sql.SQLOutput;
import java.time.chrono.ThaiBuddhistChronology;
import java.util.*;

/**
 * Created By zby on 20:39 2019/2/13
 */

public class MethodHelper {

    private static Logger logger = LoggerFactory.getLogger(MethodHelper.class);

    /**
     * Created By zby on 20:44 2019/2/13
     * 将属性转化为get方法
     *
     * @param props 属性名称
     */
    public static String propsToGetMethod(String props) {
        if (StringHelper.isBlank(props))
            return null;
        return "get" + props.substring(0, 1).toUpperCase() + props.substring(1);
    }

    /**
     * Created By zby on 18:36 2019/2/16
     * 将属性转化为get方法
     *
     * @param props    属性名称
     * @param propType 属性类型
     */
    public static String propsToSetMethod(String props, Class<?> propType) {
        if (StringUtils.isBlank(props) || null == propType)
            throw new RuntimeException("参数不能为空props=" + props + ",propType=" + propType);
        String typeName = propType.getSimpleName();
        if (!("boolean".equalsIgnoreCase(typeName)))
            return "set" + props.substring(0, 1).toUpperCase() + props.substring(1);
        return null;
    }

    /**
     * Created By zby on 17:37 2019/3/2
     *
     * @param methodName 方法名
     */
    public static String methodNameToProName(String methodName) {
        if (StringUtils.isBlank(methodName)) {
            throw new RuntimeException("methodName为空");
        }
        methodName = StringUtils.substring(methodName, 3);
        return StringUtils.substring(methodName, 0, 1).toLowerCase() + StringUtils.substring(methodName, 1);
    }

    /**
     * Created By zby on 23:30 2019/2/16
     * 过滤method方法
     *
     * @param methods 方法集合
     * @param cond    条件
     */
    public static Set<Method> filterMethods(Method[] methods, String cond) {
        if (StringUtils.isBlank(cond)) {
            throw new RuntimeException("条件不能为空");
        }
        Set<Method> methodSet = null;
        if (ArrayUtil.isNotEmpty(methods)) {
            methodSet = new HashSet<>();
            for (Method method : methods) {
                String methodName = method.getName();
                if (methodName.startsWith(cond)) {
                    methodSet.add(method);
                }
            }
        }
        return methodSet;
    }


    /**
     * Created By zby on 20:29 2019/3/2
     * 过滤set 方法
     */
    public static List<Method> filterSetMethods(Method[] methods) {
        Set<Method> methodSet = filterMethods(methods, "set");
        List<Method> tables = new ArrayList();
        for (Iterator iterator = methodSet.iterator(); iterator.hasNext(); ) {
            tables.add((Method) iterator.next());
        }
        return tables;
    }

    /**
     * Created By zby on 22:10 2019/3/2
     * 移除方法
     */
    public static List<Method> removeMethod(List<Method> src, List<Method> del) {
        if (CollectionUtil.isEmpty(src)) {
            throw new RuntimeException("原集合为空src=" + src);
        }
        if (CollectionUtil.isNotEmpty(src) && CollectionUtil.isEmpty(del)) {
            return src;
        }
        if (CollectionUtil.isNotEmpty(del) && del.size() > src.size()) {
            throw new RuntimeException("移除集合的长度不能大于待移除集合");
        }
        for (Iterator srcIt = src.iterator(); srcIt.hasNext(); ) {
            Method srcMethod = (Method) srcIt.next();
            for (Method method : del) {
                if (method.equals(srcMethod)) {
                    srcIt.remove();
                }
            }
        }
        return src;
    }
}
